package com.eostoken.eos_wallet.ui.activity;

import android.os.Bundle;
import android.text.InputFilter;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.FragmentUtils;
import com.eostoken.eos_wallet.R;
import com.eostoken.eos_wallet.base.BaseActivity;
import com.eostoken.eos_wallet.db.MgpWallet;
import com.eostoken.eos_wallet.ui.fragment.ActiveFragment;
import com.eostoken.eos_wallet.ui.fragment.ChangePasswordFragment;
import com.eostoken.eos_wallet.ui.fragment.ImportWalletFragment;
import com.eostoken.eos_wallet.ui.fragment.PermissionsViewFragment;
import com.eostoken.eos_wallet.ui.fragment.ResourceAdministrationFragment;
import com.eostoken.eos_wallet.ui.fragment.TransferFragment;
import com.eostoken.eos_wallet.ui.fragment.WalletFunctionFragment;
import com.eostoken.eos_wallet.ui.fragmentfactory.FragmentFactory;
import com.eostoken.eos_wallet.utils.CashierInputFilter;
import com.fredy.mvp.BasePresenter;
import com.fredy.mvp.BaseView;
import com.fredy.mvp.Model;
import com.google.android.material.textfield.TextInputEditText;

import static com.eostoken.eos_wallet.utils.Constants.ACTIVATE_ACCOUNT;
import static com.eostoken.eos_wallet.utils.Constants.ACTIVE;
import static com.eostoken.eos_wallet.utils.Constants.BUY_SELL_RAM;
import static com.eostoken.eos_wallet.utils.Constants.CHANGE_PASSWORD;
import static com.eostoken.eos_wallet.utils.Constants.CREATE_WALLET;
import static com.eostoken.eos_wallet.utils.Constants.EXTRA_ACCOUNT_INFO;
import static com.eostoken.eos_wallet.utils.Constants.EXTRA_INTERSECTION;
import static com.eostoken.eos_wallet.utils.Constants.EXTRA_TRANSACTION_LIST;
import static com.eostoken.eos_wallet.utils.Constants.EXTRA_WALLET;
import static com.eostoken.eos_wallet.utils.Constants.IMPORT_WALLET;
import static com.eostoken.eos_wallet.utils.Constants.INTENT_EXTRA_KEY_QR_SCAN;
import static com.eostoken.eos_wallet.utils.Constants.PERMISSIONS_VIEW;
import static com.eostoken.eos_wallet.utils.Constants.RESOURCE_ADMINISTRATION;
import static com.eostoken.eos_wallet.utils.Constants.TRANSACTION;
import static com.eostoken.eos_wallet.utils.Constants.TRANSFER;
import static com.eostoken.eos_wallet.utils.Constants.WALLET_FUNCTION;
import static com.eostoken.eos_wallet.utils.Constants.WALLET_INFO;

public class BaseWalletActivity extends BaseActivity {

    private String accountInfoJSON;
    private String accountName;
    public static BaseWalletActivity activity;
    public CashierInputFilter filter;
    private int type;

    @Override
    protected void initData() {
        activity = this;
        type = getIntent().getIntExtra(EXTRA_INTERSECTION, -1);
        filter = new CashierInputFilter();
    }

    @Override
    public void initView() {
        BarUtils.setStatusBarColor(this, ContextCompat.getColor(this, R.color.app_color_blue));
        BarUtils.setStatusBarLightMode(this, false);
        Fragment fragment = null;
        Bundle bundle = null;
        String scanResult;
        switch (type) {
            case RESOURCE_ADMINISTRATION:
                accountInfoJSON = getIntent().getStringExtra(EXTRA_ACCOUNT_INFO);
                fragment = FragmentFactory.getInstatic().getFragment(RESOURCE_ADMINISTRATION);
                bundle = new Bundle();
                bundle.putString(EXTRA_ACCOUNT_INFO, accountInfoJSON);
                fragment.setArguments(bundle);
                break;
            case WALLET_INFO:
                accountInfoJSON = getIntent().getStringExtra(EXTRA_ACCOUNT_INFO);
                boolean isTransactionList = getIntent().getBooleanExtra(EXTRA_TRANSACTION_LIST, false);
                fragment = FragmentFactory.getInstatic().getFragment(WALLET_FUNCTION);
                bundle = new Bundle();
                bundle.putString(EXTRA_ACCOUNT_INFO, accountInfoJSON);
                bundle.putBoolean(EXTRA_TRANSACTION_LIST, isTransactionList);
                fragment.setArguments(bundle);
                break;
            case ACTIVATE_ACCOUNT:
                MgpWallet wallet1 = getIntent().getParcelableExtra(EXTRA_WALLET);
                fragment = FragmentFactory.getInstatic().getFragment(ACTIVE);
                bundle = new Bundle();
                bundle.putParcelable(EXTRA_WALLET, wallet1);
                bundle.putBoolean("isCreate", false);
                fragment.setArguments(bundle);
                break;
            case CHANGE_PASSWORD:
                fragment = FragmentFactory.getInstatic().getFragment(CHANGE_PASSWORD);
                break;
            case PERMISSIONS_VIEW:
                accountInfoJSON = getIntent().getStringExtra(EXTRA_ACCOUNT_INFO);
                fragment = FragmentFactory.getInstatic().getFragment(PERMISSIONS_VIEW);
                bundle = new Bundle();
                bundle.putString(EXTRA_ACCOUNT_INFO, accountInfoJSON);
                fragment.setArguments(bundle);
                break;
            case TRANSFER:
                scanResult = getIntent().getStringExtra(INTENT_EXTRA_KEY_QR_SCAN);
                fragment = FragmentFactory.getInstatic().getFragment(TRANSACTION);
                bundle = new Bundle();
                bundle.putString(INTENT_EXTRA_KEY_QR_SCAN, scanResult);
                fragment.setArguments(bundle);
                break;
            case IMPORT_WALLET:
                scanResult = getIntent().getStringExtra(INTENT_EXTRA_KEY_QR_SCAN);
                fragment = FragmentFactory.getInstatic().getFragment(IMPORT_WALLET);
                bundle = new Bundle();
                bundle.putString(INTENT_EXTRA_KEY_QR_SCAN, scanResult);
                fragment.setArguments(bundle);
                break;
        }

        if (fragment != null) {
            FragmentUtils.add(getSupportFragmentManager(), fragment, R.id.frag_container);
        }
    }

    @Override
    public int getContentView() {
        return R.layout.activity_child;
    }

    @Override
    public Model createModel() {
        return null;
    }

    @Override
    public BaseView createView() {
        return null;
    }

    @Override
    public BasePresenter createPresenter() {
        return null;
    }

    public void unEditableEditText(TextInputEditText editText) {
        editText.setEnabled(false);//去掉点击时编辑框下面横线:
        editText.setFocusable(false);//不可编辑
        editText.setFocusableInTouchMode(false);//不可编辑
    }

    public void cashierFiltersEditText(TextInputEditText editText) {
        InputFilter[] filters = {filter};
        editText.setFilters(filters);
    }

    @Override
    public boolean isShowState() {
        return true;
    }
}
